for loop in C

The for loop in C language is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like the array and linked list.

Syntax of for loop in C

The syntax of for loop in c language is given below:
    
        for(Expression 1; Expression 2; Expression 3){  
            //code to be executed  
            }  
         
    
            

C for loop Examples

Let's see the simple program of for loop that prints table of 1.
    
        #include  
            int main(){  
            int i=0;        
            for(i=1;i<=10;i++){      
            printf("%d n",i);      
            }     
            return 0;  
            }     
    
            

Output

1 2 3 4 5 6 7 8 9 10

C Program: Print table for the given number using C for loop


    #include  
        int main(){  
        int i=1,number=0;      
        printf("Enter a number: ");    
        scanf("%d",&number);    
        for(i=1;i<=10;i++){      
        printf("%d n",(number*i));    
        }    
        return 0;  
        }    

        

Output

Enter a number: 2 2 4 6 8 10 12 14 16 18 20 Enter a number: 1000 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000

Properties of Expression 1

Example 1

    
        #include   
            int main()  
            {  
                int a,b,c;  
                for(a=0,b=12,c=23;a<2;a++)  
                {  
                    printf("%d ",a+b+c);  
                }  
            }  
    
            

Output

35 36

Example 2

    
        #include   
            int main()  
            {  
                int i=1;  
                for(;i<5;i++)  
                {  
                    printf("%d ",i);  
                }  
            }  
    
            

Output

1 2 3 4

Properties of Expression 2

Example 1

            
                #include   
                    int main()  
                    {  
                        int i;  
                        for(i=0;i<=4;i++)  
                        {  
                            printf("%d ",i);  
                        }  
                    }  
            
                    

Output

0 1 2 3 4

Example 2

                
                    #include   
                        int main()  
                        {  
                            int i,j,k;  
                            for(i=0,j=0,k=0;i<4,k<8,j<10;i++)  
                            {  
                                printf("%d %d %dn",i,j,k);  
                                j+=2;  
                                k+=3;  
                            }  
                        }     
                
                        

Output

0 0 0 1 2 3 2 4 6 3 6 9 4 8 12

Example 3

                
                    #include   
                        int main()  
                        {  
                            int i;  
                            for(i=0;;i++)  
                            {  
                                printf("%d",i);  
                            }  
                        }   
                
                        

Output

infinite loop

Properties of Expression 3

Example 1

                
                    #include  
                        void main ()  
                        {  
                            int i=0,j=2;  
                            for(i = 0;i<5;i++,j=j+2)  
                            {  
                                printf("%d %dn",i,j);  
                            }  
                        }  
                
                        

Output

0 2 1 4 2 6 3 8 4 10

Loop body

The braces {} are used to define the scope of the loop. However, if the loop contains only one statement, then we don't need to use braces. A loop without a body is possible. The braces work as a block separator, i.e., the value variable declared inside for loop is valid only for that block and not outside. Consider the following example.

    #include  
        void main ()  
        {  
            int i;  
            for(i=0;i<10;i++)  
            {  
                int i = 20;  
                printf("%d ",i);  
            }  
        }  

        

Output

20 20 20 20 20 20 20 20 20 20

Infinitive for loop in C

To make a for loop infinite, we need not give any expression in the syntax. Instead of that, we need to provide two semicolons to validate the syntax of the for loop. This will work as an infinite for loop.

    #include  
        void main ()  
        {  
            for(;;)  
            {  
                printf("welcome to javatpoint");  
            }  
        }   

        
If you run this program, you will see above statement infinite times.